package com.dz.lm.web.controller.comp;

import com.alibaba.fastjson.JSONObject;
import com.dz.lm.common.exception.ServiceException;
import com.dz.lm.common.util.FileUploadUtil;
import com.dz.lm.common.util.TecentVideo;
import com.dz.lm.common.utils.StringUtils;
import com.dz.lm.common.utils.json.JsonUtils;
import com.dz.lm.domain.comp.Comp;
import com.dz.lm.service.attach.AttachService;
import com.dz.lm.service.comp.comp.CompService;
import com.dz.lm.service.comp.account.CompAccountService;
import com.dz.lm.web.controller.Basic2Control;
import com.smartlbs.util.DateUtil;
import com.smartlbs.util.StringUtil;
import com.smartlbs.util.UploadFile;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.util.*;

@Controller
@RequestMapping("/rewriteUrl/comp")
public class CompController extends Basic2Control {
    public Logger logger = LogManager.getLogger(CompController.class);
    @Autowired
    private CompService compService;
    @Autowired
    private CompAccountService compAccountService;
    @Autowired
    private AttachService attachService;
    /**
     * 注册公司
     * @param request
     * @return
     * @throws ServiceException
     */
    @ResponseBody
    @RequestMapping("/register")
    public Map add(HttpServletRequest request) throws ServiceException {
        String result = "01";
        String message = "";
        Object data = null;
        JSONObject subjo = new JSONObject();
        String reqData = request.getAttribute("_reqData") != null ? request.getAttribute("_reqData").toString() : "";
        Map<String, Object> param = JsonUtils.readValue(reqData.toString());
        Object imgUrl = request.getAttribute("imgUrl");
        //附件
        List<UploadFile> fList = (List<UploadFile>) request.getAttribute("fileField");

        Map<String, Object> urlMap = new HashMap<String, Object>();
        if (fList != null && fList.size()>0) {
            urlMap = FileUploadUtil.uploadByInStream(fList, imgUrl, "comp");
        }

        //公司名称
        String compName = param.get("compName") != null ? param.get("compName").toString() : "";
        //手机号
        String phone = param.get("phone") != null ? param.get("phone").toString() : "";

        //内容
        String content = param.get("content") != null ? param.get("content").toString() : "";
        //公告
        String notice = param.get("notice") != null ? param.get("notice").toString() : "";

        //地址
        String address = param.get("address") != null ? param.get("address").toString() : "";
        //邮箱
        String mail = param.get("mail") != null ? param.get("mail").toString() : "";
        //用户名
        String userName = param.get("userName") != null ? param.get("userName").toString() : "";
        //密码
        String password = param.get("password") != null ? param.get("password").toString() : "";
        //附件id
        Integer videoId = param.get("videoId") != null ? Integer.parseInt(param.get("videoId").toString()) : 0;


        if (StringUtils.isEmpty(password) || StringUtils.isEmpty(userName)) {
            message = "参数有误";
        } else {
            //账号查重
            int checking = compAccountService.checking(userName);
            if(checking > 0){
                message ="账户名重复。";
            }else {
                Comp comp = new Comp();
                comp.setCompName(compName);
                comp.setAddress(address);
                comp.setContent(content);
                comp.setMail(mail);
                comp.setPhone(phone);
                comp.setLogo(urlMap.get("logo") != null ? imgUrl + urlMap.get("logo").toString() : "");
                comp.setUrl1(urlMap.get("url1") != null ? imgUrl + urlMap.get("url1").toString() : "");
                comp.setUrl2(urlMap.get("url2") != null ? imgUrl + urlMap.get("url2").toString() : "");
                comp.setUrl3(urlMap.get("url3") != null ? imgUrl + urlMap.get("url3").toString() : "");
                comp.setUrl4(urlMap.get("url4") != null ? imgUrl + urlMap.get("url4").toString() : "");
                comp.setVideoId(videoId);
                try {
                    comp.setCreateTime(DateUtil.getNowDateTimeString());
                } catch (ParseException e) {
                    e.printStackTrace();
                }
                try {
                    comp.setCreateTime(DateUtil.getNowDateTimeString());
                } catch (ParseException e) {
                    e.printStackTrace();
                }
                String code = compService.registerComp(comp, userName, password);
                if(!StringUtil.containEmpty(code)){
                    data= code;
                    result ="00";
                    message ="注册成功";
                }
            }

        }
            subjo.put("message", message);
            subjo.put("result", result);
            subjo.put("data", data);
            return subjo;
    }

    /**
     * 详情
     * @param request
     * @return
     * @throws ServiceException
     */
    @ResponseBody
    @RequestMapping("/get")
    public Map get(HttpServletRequest request) throws ServiceException {
        String result = "01";
        String message = "";
        Object data = null;
        JSONObject subjo = new JSONObject();
        String reqData = request.getAttribute("_reqData") != null ? request.getAttribute("_reqData").toString() : "";
        Map<String, Object> param = JsonUtils.readValue(reqData.toString());
        Object imgUrl = request.getAttribute("imgUrl");
        //公司编码
        String code = param.get("code") != null ? param.get("code").toString() : "";

        Integer compId = param.get("compId") != null ? Integer.parseInt(param.get("compId").toString()) : 0;
        if(compId != 0){
            data = compService.get(compId);
            result ="00";
            message ="获取成功";
        }else if(!StringUtil.containEmpty(code)){
            data = compService.getByCode(code);
            result ="00";
            message ="获取成功";
        }else {
            message = "参数有误";
        }

        subjo.put("message", message);
        subjo.put("result", result);
        subjo.put("data", data);
        return subjo;
    }
    /**
     * 修改公司
     * @param request
     * @return
     * @throws ServiceException
     */
    @ResponseBody
    @RequestMapping("/update")
    public Map updateCode(HttpServletRequest request) throws ServiceException {
        String result = "01";
        String message = "";
        Object data = null;
        JSONObject subjo = new JSONObject();
        String reqData = request.getAttribute("_reqData") != null ? request.getAttribute("_reqData").toString() : "";
        Map<String, Object> param = JsonUtils.readValue(reqData.toString());
        Object imgUrl = request.getAttribute("imgUrl");
        //附件
        List<UploadFile> fList = (List<UploadFile>) request.getAttribute("fileField");

        Map<String, Object> urlMap = new HashMap<String, Object>();
        if (fList != null && fList.size()>0) {
            urlMap = FileUploadUtil.uploadByInStream(fList, imgUrl, "comp");
        }

        //公司编码
        String code = param.get("code") != null ? param.get("code").toString() : "";
        //公司名称
        String compName = param.get("compName") != null ? param.get("compName").toString() : "";
        //地址
        String address = param.get("address") != null ? param.get("address").toString() : "";
        //内容
        String content = param.get("content") != null ? param.get("content").toString() : "";
        //公告
        String notice = param.get("notice") != null ? param.get("notice").toString() : "";
        //删除视频id
        String deFileId = param.get("deFileId") != null ? param.get("deFileId").toString() : "";
        //删除附件表id
        Integer deVideoId = param.get("deVideoId") != null ? Integer.parseInt(param.get("deVideoId").toString()) : 0;
        //附件id
        Integer videoId = param.get("videoId") != null ? Integer.parseInt(param.get("videoId").toString()) : 0;
        //删除附件地址,逗号分隔
        String deFiles = param.get("deFiles") != null ? param.get("deFiles").toString() : "";

        Integer compId = param.get("compId") != null ? Integer.parseInt(param.get("compId").toString()) : 0;
        String url1 = param.get("url1") != null ? param.get("url1").toString() : "";
        String url2 = param.get("url2") != null ? param.get("url2").toString() : "";
        String url3 = param.get("url3") != null ? param.get("url3").toString() : "";
        String url4 = param.get("url4") != null ? param.get("url4").toString() : "";
        String logo = param.get("logo") != null ? param.get("logo").toString() : "";
        if( compId ==0){
            message = "参数有误";
        }else {
            Comp comp = new Comp();
            comp.setCompName(compName);
            comp.setAddress(address);
            comp.setCompId(compId);
            comp.setCode(code);
            comp.setVideoId(videoId);
            comp.setContent(content);
            comp.setNotice(notice);
            comp.setUrl1(url1);
            comp.setUrl2(url2);
            comp.setUrl3(url3);
            comp.setUrl4(url4);
            comp.setLogo(logo);
            if(urlMap.get("logo") != null){
                comp.setLogo(urlMap.get("logo") != null ? imgUrl + urlMap.get("logo").toString() : "");

            }
            if(urlMap.get("url1") != null){
                comp.setUrl1(urlMap.get("url1") != null ? imgUrl + urlMap.get("url1").toString() : "");

            }
            if(urlMap.get("url2") != null){
                comp.setUrl2(urlMap.get("url2") != null ? imgUrl + urlMap.get("url2").toString() : "");

            }
            if(urlMap.get("url3") != null){
                comp.setUrl3(urlMap.get("url3") != null ? imgUrl + urlMap.get("url3").toString() : "");

            }
            if(urlMap.get("url4") != null){
                comp.setUrl4(urlMap.get("url4") != null ? imgUrl + urlMap.get("url4").toString() : "");

            }
            try {
                comp.setUpdateTime(DateUtil.getNowDateTimeString());
            } catch (ParseException e) {
                e.printStackTrace();
            }
            int i = compService.updateCode(comp);
            if(i > 0){
                if(!StringUtil.containEmpty(deFiles)){
                    try {
                        for (String s : deFiles.split(",")) {
                            FileUploadUtil.deleteFile(s);
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
                if(!StringUtils.isEmpty(deFileId)){ //删除视频
                    TecentVideo.delete(deFileId);
                    attachService.delete(deVideoId);
                }
                result ="00";
                message ="修改成功";
            }
        }

        subjo.put("message", message);
        subjo.put("result", result);
        subjo.put("data", data);
        return subjo;
    }
}
